package coreClassLibrary;

// 抽象类示例 - 动物
abstract class Animal {
    protected String name;
    
    // 构造方法
    public Animal(String name) {
        this.name = name;
    }
    
    // 普通方法
    public void sleep() {
        System.out.println(name + "正在睡觉");
    }
    
    // 抽象方法 - 必须由子类实现
    abstract void makeSound();
    abstract void eat();
}

// 接口示例 - 可飞行的
interface Flyable {
    // 接口中的方法默认是 public abstract
    void fly();
    
    // Java 8 之后接口可以有默认方法
    default void glide() {
        System.out.println("滑翔中...");
    }
}

// 接口示例 - 可游泳的
interface Swimmable {
    void swim();
}

// 实现抽象类的具体类
class Bird extends Animal implements Flyable {
    public Bird(String name) {
        super(name);
    }
    
    @Override
    void makeSound() {
        System.out.println(name + "在唱歌：啾啾啾");
    }
    
    @Override
    void eat() {
        System.out.println(name + "在吃虫子");
    }
    
    @Override
    public void fly() {
        System.out.println(name + "展翅高飞");
    }
}

// 实现抽象类和多个接口的具体类
class Duck extends Animal implements Flyable, Swimmable {
    public Duck(String name) {
        super(name);
    }
    
    @Override
    void makeSound() {
        System.out.println(name + "在叫：嘎嘎嘎");
    }
    
    @Override
    void eat() {
        System.out.println(name + "在吃小鱼");
    }
    
    @Override
    public void fly() {
        System.out.println(name + "飞行中");
    }
    
    @Override
    public void swim() {
        System.out.println(name + "在水中游泳");
    }
}

// 主类
public class AbstractAndInterface {
    public static void main(String[] args) {
        Bird sparrow = new Bird("麻雀");
        sparrow.makeSound();
        sparrow.eat();
        sparrow.fly();
        sparrow.sleep();
        sparrow.glide();
        
        System.out.println("\n-------------------\n");
        
        Duck duck = new Duck("小鸭子");
        duck.makeSound();
        duck.eat();
        duck.fly();
        duck.swim();
        duck.sleep();
    }
} 